package com.jinlei.crm.workbench.controller;

import com.jinlei.crm.commons.utils.DateUtil;
import com.jinlei.crm.commons.utils.Result;
import com.jinlei.crm.commons.utils.UUIDUtil;
import com.jinlei.crm.settings.entity.User;
import com.jinlei.crm.settings.service.UserService;
import com.jinlei.crm.workbench.entity.Activity;
import com.jinlei.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 市场活动表(Activity)表控制层
 *
 * @author makejava
 * @since 2022-07-02 15:05:33
 */
@Controller
@RequestMapping("activity")
public class ActivityController {
    /**
     * 服务对象
     */
    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @param pageNumber    当前页码
     * @param pageSize      每页条数
     * @param activity      分页对象
     * @return 查询结果
     */
    @GetMapping("/page")
    @ResponseBody
    public Result listPage(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                           Activity activity){
        return Result.success(activityService.ListPage(pageNumber, pageSize, activity));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/find/{id}")
    @ResponseBody
    public Result findById(@PathVariable("id") String id) {
        return Result.success(activityService.findById(id));
    }

    /**
     * 通过主键查询数据详情
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, HttpServletRequest request) {
        request.setAttribute("activity", activityService.findById(id));
        Activity activity = activityService.findById(id);
        System.out.println(activity);
        return "workbench/activity/detail";
    }

    /**
     * 新增数据
     *
     * @param activity 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody Activity activity, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        //设置手动维护id
        activity.setId(UUIDUtil.getUUID());
        // 设置所有者
        activity.setUserName(userService.findById(activity.getOwner()).getName());
        // 设置创建者，修改者
        activity.setCreateBy(loginUser.getName() + "  " + "(" + loginUser.getLoginAct() + ")");
        activity.setEditBy(loginUser.getName() + "  " + "(" + loginUser.getLoginAct() + ")");
        // 设置创建时间  修改时间
        activity.setCreateTime(DateUtil.formatDate(new Date()));
        activity.setEditTime(DateUtil.formatDate(new Date()));
        activityService.save(activity);
        return Result.success();
    }

    /**
     * 编辑数据
     *
     * @param activity 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody Activity activity, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        // 设置所有者
        activity.setUserName(userService.findById(activity.getOwner()).getName());
        activity.setEditBy(loginUser.getName() + "  " + "(" + loginUser.getLoginAct() + ")");
        activity.setEditTime(DateUtil.formatDate(new Date()));
        activityService.edit(activity);
        return Result.success();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id") String id) {
        activityService.deleteById(id);
        return Result.success();
    }

    /**
     * 通过主键批量删除数据
     *
     * @param ids 主键数组
     * @return 影响行数
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public Result deleteById(String[] ids) {
        activityService.deleteBatch(ids);
        return Result.success();
    }


    @GetMapping("/select/{clueId}")
    @ResponseBody
    public Result selectByClueId(@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "100") Integer pageSize,
                                 @PathVariable("clueId") String clueId) {
        return Result.success(activityService.selectByClueId(pageNumber,pageSize,clueId));
    }


    @GetMapping("/pageList/{clueId}")
    @ResponseBody
    public Result pageList(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                           @RequestParam(value = "pageSize",defaultValue = "100") Integer pageSize,String name,@PathVariable("clueId") String clueId) {
        return Result.success(activityService.selectList(pageNumber,pageSize,name,clueId));
    }
}

